
export default [
    {id:'resourceName',label:'条目',width:'*',value:'',required:true,sortable:true,editorType:'input',show:true}
]

let extendFields=[
    {id:'assignmentStatus',label:'是否已分配',width:170,value:'',required:true,sortable:true,editorType:'selectDrop',show:true,cellFun:(rowRecord,itemd)=>{
            if((rowRecord[itemd]+"")==="0"){
                return '未分配'
            }else{
                return '已分配';
            }
        }},
    {id:'createType',label:'创建方式',width:170,value:'',required:true,sortable:true,editorType:'selectDrop',show:true,cellFun:(rowRecord,itemd)=>{
            if((rowRecord[itemd]+"")==="0"){
                return '手动创建'
            }else{
                return '购买创建';
            }
        }},
    {id:'serverCreateTime',label:'创建时间',width:170,value:'',required:true,sortable:true,editorType:'input',show:true}
];
export {extendFields};

/**
 * 计算列表的宽度，将多余宽度进行平均分配
 * @param cols
 * @private
 */
let _initColWidth=function (cols) {
    let totalWidth=document.body.clientWidth-240-180;
    let colTotalWid=0;
    cols.forEach((item)=>{
        colTotalWid=parseFloat(colTotalWid)+parseFloat(item.width)
    });
    //如果存在多余的宽度，则将多余的宽度平均分配到其他列去
    if(colTotalWid<totalWidth){
        let oWid=totalWidth-colTotalWid;
        let oWidNum=oWid/cols.length;
        cols.forEach((itemObj)=>{
            itemObj["width"]=itemObj["width"]+oWidNum;
        })
    }
}
export {_initColWidth};


function _getSonItemJson(subItems){
    let sonJson={};
    subItems.forEach((itemObj)=>{
        if(itemObj["value"]!=null&&itemObj["value"]!==""){
            sonJson[itemObj["id"]]=itemObj["value"];
        }
    });
    return sonJson;
}

function _getSonItemAryJson(itemDatas){
    let itemDatasRet=[];
    itemDatas.forEach((subItems)=>{
        itemDatasRet.push(_getSonItemJson(subItems));
    });
    return  itemDatasRet;

}
/**
 * 组装添加时候要求的dom结构
 * @param cols
 * @param tabType
 * @returns {{resourceDetailContent: string, resourceName: *, resourceType: *}}
 * @private
 */
let _changToAddDomain=function (cols,tabType,junpFirstCol=true) {
    let dbField=[];
    cols.forEach((itemObj,index)=>{
        //排除创建时间这列
        if(itemObj.id!=="serverCreateTime"){
            if(!junpFirstCol&&index===0){
                let jsonObj={};
                jsonObj[itemObj["id"]]=itemObj["value"];
                jsonObj['type']=2+'';
                dbField.push(jsonObj);
            }
            if(index!==0){
                if(itemObj["dictionaryValueType"]+''==='1'){
                    let jsonObj={};
                    jsonObj[itemObj["id"]]=itemObj["value"];
                    jsonObj['type']=1+'';
                    dbField.push(jsonObj);
                }else if(itemObj["dictionaryValueType"]+''==='2'){
                    let jsonObj={};
                    jsonObj[itemObj["id"]]=itemObj["value"];
                    jsonObj['type']=2+'';
                    dbField.push(jsonObj);
                }else if(itemObj["dictionaryValueType"]+''==='3'){
                    let jsonObj={};
                    jsonObj[itemObj["id"]]=_getSonItemJson(itemObj["subItems"]);
                    jsonObj['type']=3+'';
                    dbField.push(jsonObj);
                }else if(itemObj["dictionaryValueType"]+''==='4'){
                    let jsonObj={};
                    jsonObj[itemObj["id"]]=_getSonItemAryJson(itemObj["itemDatas"]);
                    jsonObj['type']=4+'';
                    dbField.push(jsonObj);
                }
            }
        }
    })
   const doaminObj= {
    	"resourceName":cols[0]["value"],
    	"resourceDetailContent":JSON.stringify(dbField),
    	"resourceType":tabType
   };
   return doaminObj;
}

export {_changToAddDomain};

/**
 * 根据数据库配置的枚举字符串获取下拉选项数组
 * @param str
 * @returns {Array}
 * @private
 */
function _getOptionsByStr(str){
    let options=[];
    str.split(";").forEach((strName)=>{
        let strNameAry=strName.split("=");
        if(strNameAry.length>1){
            options.push({value:strNameAry[0],label:strNameAry[1]});
        }else{
            options.push({value:strName,label:strName});
        }
    })
    return options;
}


function _getValueMap(options){
    let map={};
    options.forEach((itemObj)=>{
        map[itemObj["value"]]=itemObj["label"];
    })
    return map;
}
function _getFieldType(typeVal) {
    if(typeVal+""==='1'){
        return 'input';
    }else   if(typeVal+""==='2'){
        return 'selectDrop';
    }else   if(typeVal+""==='3'){
        return 'group';
    }else   if(typeVal+""==='4'){
        return 'groupArray';
    }
}

/**
 * 下拉组合下 下拉项
 * @param sourceValues
 * @returns {Array}
 * @private
 */
function _getOptionsBySourceValue(sourceValues) {
    let options=[];
    sourceValues.forEach((sonItemObj)=>{
        for(let prop in sonItemObj){
            let {dictionaryKey,dictionaryValue}=sonItemObj[prop];
            options.push({value:dictionaryKey,label:dictionaryValue});
        }
    })
    return options;
}

function _getJosnMapBySourceValue(sourceValues) {
    let options={};
    sourceValues.forEach((sonItemObj)=>{
        for(let prop in sonItemObj){
            let {dictionaryKey,dictionaryValue}=sonItemObj[prop];
            options[dictionaryKey]=dictionaryValue;
        }
    })
    return options;
}
function _getColWidth(dictionaryName){
    if(dictionaryName==null||dictionaryName===""){
        return 100;
    }
    let totalWid=parseFloat(dictionaryName.length)*15+40;
    if(totalWid<100){
        return 100;
    }
    return totalWid;
}
/**
 * 获取组合 组合数组下的 子元素
 * @param dictionaryValueType
 * @param childrenSourceValue
 * @returns {Array}
 * @private
 */
function _getSubItems(dictionaryValueType,childrenSourceValue) {
    if(dictionaryValueType+""==="4"){
        childrenSourceValue=childrenSourceValue[0];
    }
    if(childrenSourceValue!=null){
        let subItems=[];
        if((dictionaryValueType+'')==="3"||(dictionaryValueType+'')==="4"){
            for(let prop in childrenSourceValue){
                if(typeof(childrenSourceValue[prop])==="object"){
                    //如果是下拉类型的数据
                    if(childrenSourceValue[prop]["sourceValue"]!=null){
                        let {dictionaryCode, dictionaryName, showAttributeStatus, dictionaryValueType,dictionaryValues} = childrenSourceValue[prop]["CommonTenantResourceDictionary"];
                        let fieldObj={
                            id: dictionaryCode,
                            label: dictionaryName,
                            width: _getColWidth(dictionaryName),
                            value: '',
                            show:true,
                            required: false,
                            sortable: true,
                            dictionaryValueType:2,
                            editorType: 'selectDrop',
                            options:_getOptionsBySourceValue(childrenSourceValue[prop]["sourceValue"]),
                            optionsMap:_getJosnMapBySourceValue(childrenSourceValue[prop]["sourceValue"])
                        };
                        subItems.push(fieldObj);
                    }else{
                        //如果是普通输入类型的数据
                        let {dictionaryCode, dictionaryName, showAttributeStatus, dictionaryValueType,dictionaryValues} = childrenSourceValue[prop];
                        let fieldObj={
                            id: dictionaryCode,
                            label: dictionaryName,
                            width: _getColWidth(dictionaryName),
                            value: '',
                            show:true,
                            required: false,
                            sortable: true,
                            dictionaryValueType:dictionaryValueType,
                            editorType: _getFieldType(dictionaryValueType),
                            options:_getOptionsByStr(dictionaryValues),
                            optionsMap:_getValueMap(_getOptionsByStr(dictionaryValues))
                        };
                        subItems.push(fieldObj);
                    }
                }
            }
        }
        return subItems;
    }
}

/**
 * 将第一个字符变成大写
 * @param str
 * @returns {string|*}
 */
function changeFirstCharToLow(str) {
    if (str!=null&&str!=="") {
        var firstChar = str.substring(0, 1);
        var afterChar = str.substring(1);
        str = firstChar.toLowerCase() + afterChar;
        return str;
    }
}

export {changeFirstCharToLow};
/**
 * 根据数据库定义的数据字典字段 获取对应的实体字段信息
 * @param res
 * @param newCols
 * @private
 */
function _getFieldFromDb(res, formCols,gridCols=[]) {
    if (res["showField"]) {
        let showFieldAry = [];
        if (typeof (res["showField"]) === "string") {
            showFieldAry = JSON.parse(res["showField"]);
        }
        showFieldAry.forEach((fieldObj) => {
            let {dictionaryCode, dictionaryName, showAttributeStatus, dictionaryValueType,dictionaryValues} = fieldObj["CommonTenantResourceDictionary"];
            let childrenSourceValue=fieldObj["childrenSourceValue"];
            let fieldObjDb={
                id: dictionaryCode,
                label: dictionaryName,
                width:_getColWidth(dictionaryName),
                value: '',
                show:true,
                required: false,
                sortable: true,
                dictionaryValueType:dictionaryValueType,
                subItems:_getSubItems(dictionaryValueType,childrenSourceValue),
                editorType: _getFieldType(dictionaryValueType),
                options:_getOptionsByStr(dictionaryValues),
                optionsMap:_getValueMap(_getOptionsByStr(dictionaryValues))
            };

            if((dictionaryValueType+'')==="4"){
                fieldObjDb["itemDatas"]=[fieldObjDb["subItems"]];
            }

            if((showAttributeStatus+"") ==="1"&&(dictionaryValueType+'')!=='3'&&(dictionaryValueType+'')!=='4'){
                gridCols.push(fieldObjDb);
            }

            formCols.push(fieldObjDb);
        });
    }
}

export {_getFieldFromDb};


function _getContentJsonObj(dbRecord) {
    let contentJson = {};
    let detailContent = JSON.parse(dbRecord["resourceDetailContent"]);
    detailContent.forEach((itemDbObj) => {
        contentJson = {...contentJson, ...itemDbObj};
    });
    return contentJson;
}

/**
 * 将数据库的记录转换成 json格式的对象
 * @param itemObj
 * @returns {{}&{assignmentStatus: *, serverCreateTime: *, resourceName: *, createType: *, id: *}}
 * @private
 * assignmentStatus 是否分配
 * resourceName：条目
 */
function _changeDbRecordToJson(itemObj){
    let contentJson = _getContentJsonObj(itemObj);
    let newJson={id:itemObj["id"],assignmentStatus:itemObj["assignmentStatus"],resourceName:itemObj["resourceName"],serverCreateTime:itemObj["serverCreateTime"],createType:itemObj["createType"],...contentJson};
    return newJson
}
export {_changeDbRecordToJson};




/**
 * 将数据集库的一条记录 转换成json对象
 * @param res
 */
function changeDbFieldToJson(res) {
    let dbRecord = res.data["commonTenantResource"];
    //将数据库的记录转换成 json格式的对象
    dbRecord = _changeDbRecordToJson(dbRecord);
    //循环设置值
    if(this.formCols==null){
        this.formCols=this.cols;
    }
    this.formCols.forEach((item) => {
        //如果是组合模式 数据格式{ "ypanSize": "33", "guige": "33" }
        if(item["dictionaryValueType"]+""==="3"){
            item["subItems"].forEach((sonItem)=>{
                sonItem["value"]=dbRecord[item["id"]]&&dbRecord[item["id"]][sonItem["id"]];
            })
        }else   if(item["dictionaryValueType"]+""==="4"){
            //如果是组合数组   数据格式如下[ { "ypanSize": "33", "guige": "33" }, { "ypanSize": "44", "guige": "44" }, { "ypanSize": "5", "guige": "5" } ]
            item["itemDatas"]=[];
            dbRecord[item["id"]].forEach((itemAObj)=>{
                let newSubitms=[];
                item["subItems"].forEach((itemObj)=>{
                    let newItem={...itemObj};
                    newItem["value"]=itemAObj[newItem["id"]];
                    newSubitms.push(newItem);
                })
                item["itemDatas"].push(newSubitms);
            })
        }else{
            //如果是其他模式
            this.curRecordId = dbRecord["id"];
            item["value"] = dbRecord[item["id"]];
        }
    });
}

export {changeDbFieldToJson};